package com.zheng1.envii.ui.components

import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.zheng1.envii.R
import com.zheng1.envii.ui.theme.EnvIITheme

enum class DeviceStatus{
    DISCONNECTED,
    CONNECTED,
    CONNECTING
}

@Composable
fun DeviceCard(
    modifier: Modifier = Modifier,
    deviceName: String,
    deviceAddress : String,
    deviceRssi: String,
    deviceConnectSatus: DeviceStatus,
    onClick: ()->Unit,
    onButtonClick: () -> Unit
){
    ElevatedCard(
        modifier = modifier
            .fillMaxWidth()
            .padding(horizontal = 8.dp, vertical = 4.dp),
        colors = CardDefaults.elevatedCardColors(
            containerColor = MaterialTheme.colorScheme.background
        ),
        onClick = onClick
    ) {
        Row(modifier = Modifier
            .height(80.dp)
            .fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Start) {
            Image(modifier = Modifier
                .height(100.dp)
                .background(MaterialTheme.colorScheme.primary),
                contentScale = ContentScale.FillWidth,
                alignment = Alignment.Center,
                painter = painterResource(R.drawable.baseline_bluetooth_24),
                contentDescription = "bluetooth"
            )
            Row ( modifier = modifier.fillMaxSize().padding(horizontal = 16.dp),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween
            ){
                var enableButton by remember {
                    mutableStateOf(true)
                }

                when(deviceConnectSatus){
                    DeviceStatus.CONNECTED -> {
                        enableButton = true
                    }
                    else ->{}
                }

                Column( modifier = Modifier.fillMaxHeight(),
                    verticalArrangement = Arrangement.SpaceAround
                ) {
                    Text(text = deviceName, fontSize = 20.sp)
                    Text(text = deviceAddress, fontSize = 12.sp)
                    Row{
                        Text(text = "RSSI:", fontSize = 10.sp)
                        Text(text = deviceRssi, fontSize = 10.sp)
                        Text(text = "dB", fontSize = 10.sp, modifier = Modifier.padding(start = 2.dp))
                    }
                }
                Button(
                    modifier = Modifier
                        .padding(start = 50.dp)
                        .width(100.dp)
                        .height(40.dp),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = MaterialTheme.colorScheme.primary,
                    ),
                    enabled = enableButton,
                    onClick = {
                        enableButton = false
                        onButtonClick.invoke()
                    }
                ) {
                    when(deviceConnectSatus){
                        DeviceStatus.CONNECTED -> {
                            Text(text = "断开", fontSize = 12.sp)
                        }
                        DeviceStatus.CONNECTING -> {
                            Text(text = "连接中", fontSize = 12.sp)
                        }
                        DeviceStatus.DISCONNECTED -> {
                            Text(text = "连接", fontSize = 12.sp)
                        }
                    }
                }
            }
        }
    }
}


@Preview(showBackground = true)
@Composable
fun DeviceCardPreview(){

    EnvIITheme(dynamicColor = false, darkTheme = false) {
        DeviceCard(
            deviceName = "EnvII",
            deviceAddress = "34:B7:DA:5B:4B:62",
            deviceRssi = "-50",
            deviceConnectSatus = DeviceStatus.CONNECTED,
            onButtonClick = {},
            onClick = {}
        )
    }
}